<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="对账批次">
        <el-input v-model="queryParams.checkBatchNo" placeholder=""></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <div>
      <el-table v-loading="loading" :data="clearingList" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="结算的对账批次号" align="center" prop="checkBatchNo" />
        <el-table-column label="商户名称" align="center" prop="businessName" />
        <el-table-column label="结算金额" align="center" prop="amount" />
        <el-table-column label="交易日期" align="center" prop="tradeDateZf" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.tradeDateZf, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <p align="right"><el-button type="warning" @click="toConfirmSettlement">确认结算</el-button></p>
    </div>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算对话框 -->
    <!--<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="对账年月日(yyyymmdd)" prop="checkDay">
          <el-input v-model="form.checkDay" placeholder="请输入对账年月日(yyyymmdd)" />
        </el-form-item>
        <el-form-item label="结算的对账批次ID" prop="checkBatchId">
          <el-input v-model="form.checkBatchId" placeholder="请输入结算的对账批次ID" />
        </el-form-item>
        <el-form-item label="结算的对账批次号" prop="checkBatchNo">
          <el-input v-model="form.checkBatchNo" placeholder="请输入结算的对账批次号" />
        </el-form-item>
        <el-form-item label="结算的对账批次明细ID" prop="checkBatchDetailId">
          <el-input v-model="form.checkBatchDetailId" placeholder="请输入结算的对账批次明细ID" />
        </el-form-item>
        <el-form-item label="交易流水号，支付订单的支付订单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入交易流水号，支付订单的支付订单号" />
        </el-form-item>
        <el-form-item label="渠道名称(拉卡拉账单类型) 1:代付" prop="billType">
          <el-select v-model="form.billType" placeholder="请选择渠道名称(拉卡拉账单类型) 1:代付">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="对账确认时间" prop="checkConfirmDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.checkConfirmDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择对账确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入结算金额" />
        </el-form-item>
        <el-form-item label="交易日期" prop="tradeDateZf">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.tradeDateZf"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择交易日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算确认状态" prop="clearingConfirmState">
          <el-input v-model="form.clearingConfirmState" placeholder="请输入结算确认状态" />
        </el-form-item>
        <el-form-item label="结算确认人" prop="clearingConfirmUserId">
          <el-input v-model="form.clearingConfirmUserId" placeholder="请输入结算确认人" />
        </el-form-item>
        <el-form-item label="结算确认时间" prop="clearingConfirmDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.clearingConfirmDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结算确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留字段5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
        <el-form-item label="删除标识,0:未删除，1：删除" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识,0:未删除，1：删除" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户ID" />
        </el-form-item>
        <el-form-item label="更新用户ID" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
import { listClearing, getClearing, delClearing, addClearing, updateClearing,confirmSettlement } from "@/api/finance/clearing";

export default {
  name: "Clearing",
  components: {
  },
  data() {
    return {
      clearingIds:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算表格数据
      clearingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        clearingIds:[],
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        checkDay: null,
        checkBatchId: null,
        checkBatchNo: null,
        checkBatchDetailId: null,
        payOrderNo: null,
        billType: null,
        checkConfirmDate: null,
        amount: null,
        tradeDateZf: null,
        clearingConfirmState: null,
        clearingConfirmUserId: null,
        clearingConfirmDate: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkDay: [
          { required: true, message: "对账年月日(yyyymmdd)不能为空", trigger: "blur" }
        ],
        checkBatchId: [
          { required: true, message: "结算的对账批次ID不能为空", trigger: "blur" }
        ],
        checkBatchNo: [
          { required: true, message: "结算的对账批次号不能为空", trigger: "blur" }
        ],
        checkBatchDetailId: [
          { required: true, message: "结算的对账批次明细ID不能为空", trigger: "blur" }
        ],
        checkConfirmDate: [
          { required: true, message: "对账确认时间不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "结算金额不能为空", trigger: "blur" }
        ],
        tradeDateZf: [
          { required: true, message: "交易日期不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //确认结算
    toConfirmSettlement() {
      confirmSettlement(this.clearingIds).then(res=>{
        console.log("确认结算---------------",res)
        if (res.code == 200) {
          alert("结算成功");
          this.getList();
        }
      })
    },
    /** 查询结算列表 */
    getList() {
      this.loading = true;
      listClearing(this.queryParams).then(response => {
        this.clearingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        checkDay: null,
        checkBatchId: null,
        checkBatchNo: null,
        checkBatchDetailId: null,
        payOrderNo: null,
        billType: null,
        checkConfirmDate: null,
        amount: null,
        tradeDateZf: null,
        clearingConfirmState: null,
        clearingConfirmUserId: null,
        clearingConfirmDate: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(val) {
      console.log("============================",val)
      this.clearingIds =val.map(row=>{
        return row.id
      });
      console.log(this.clearingIds)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClearing(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改结算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClearing(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClearing(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除结算编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClearing(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/clearing/export', {
        ...this.queryParams
      }, `finance_clearing.xlsx`)
    }
  }
};
</script>
